//
//  WDGoutWaitAssignTopAddressView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/7.
//

#import "WDGoutWaitAssignTopAddressView.h"
#import "WDArrivalServiceItemModel.h"

@interface WDGoutWaitAssignTopAddressView ()
@property (nonatomic, strong) WDArrivalServiceItemModel *model;

@property (nonatomic, strong) CALayer *bottomLine;

@property (nonatomic, strong) UIImageView *locImgView;

/// 地址
@property (nonatomic, strong) UILabel *adsLabel;

@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation WDGoutWaitAssignTopAddressView

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithBottomLine];
}

- (void)initWithBottomLine {
    if (!_bottomLine) {
        CGRect frame = CGRectMake(20, self.height - 1, self.width - 40, 1);
        _bottomLine = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#EEEEEE"]];
    }
}

- (void)initWithLocImgView {
    if (!_locImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_main_loc_blue"];
        CGRect frame = CGRectMake(20, (self.height - 1 - image.size.height) * 0.5, image.size.width, image.size.height);
        _locImgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.userInteractionEnabled = YES;
        }];
    }
}

- (void)initWithAdsLabel {
    if (!_adsLabel) {
        CGRect frame = CGRectMake(self.locImgView.right + 6, 0, self.bottomLine.width - self.locImgView.width - 6, 20);
        _adsLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
//            label.backgroundColor = [UIColor orangeColor];
            label.userInteractionEnabled = YES;
        }];
    }
    if (self.model) {
        self.adsLabel.centerY = self.locImgView.centerY;
        NSString *str = @"";
        if (![objectNull(self.model.rescueAddress) isEqualToString:@""]) {
            str = objectNull(self.model.rescueAddress);
        }
        if (self.model.rescueDistance && [self.model.rescueDistance integerValue] > 0) {
            str = [NSString stringWithFormat:@"%@ %zd", str, [self.model.rescueDistance integerValue]];
        }
        _adsLabel.text = str;
    }
}

- (void)updateWithViewData:(id)data {
    self.model = data;
    
    [self initWithLocImgView];
    
    [self initWithAdsLabel];
    @weakify(self);
    if (!self.tap) {
        _tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self);
            if (self.viewBlock) {
                self.viewBlock(nil);
            }
        }];
        [self addGestureRecognizer:_tap];
    }
}


@end
